package com.wxh.utils;

import javax.sql.rowset.serial.SerialClob;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.SQLException;

/**
 * @ClassName: ClobUtils
 * @Description: clob工具类
 * @Author wxh
 * @Date: 2022/4/25 14:53
 * @Version V1.0.0
 * @Since 1.8
 */
public class ClobUtils {

    private ClobUtils(){

    };

    /**
     * clob转换为string
     * @param clob clob参数
     * @return String
     */
    public static String clobToString(Clob clob){
        StringBuilder sb = new StringBuilder();
        try {
            Reader reader = clob.getCharacterStream();
            BufferedReader buffer = new BufferedReader(reader);
            String line;
            while (null != (line = buffer.readLine())){
                sb.append(line);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    /**
     * string转clob
     * @param str 字符串
     * @return Clob
     */
    public static Clob stringToClob(String str){
        try {
            Clob clob = new SerialClob(str.toCharArray());
            return clob;
        } catch (SQLException e) {
            return null;
        }
    }

    public static void main(String[] args) {
        String str = "张三李四王玉张三李四王玉张三李四王玉张三李四王玉张三李四王玉张三李四王玉张三李四王玉";
        System.out.println("转换前："+str);
        Clob clob = stringToClob(str);
        String covertStr = clobToString(clob);
        System.out.println("转换后: "+covertStr);
    }
}
